﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace BLL
{
    public class MembersBll
    {
        public const string Sold = "finaceKey";

        public Members CheckLogin(string memberName, string password)
        {
            password = FormsAuthentication.HashPasswordForStoringInConfigFile(password + Sold, "MD5");

            var result = (from a in DB.PersonalFinanceDB.Members
                          where a.MemberName == memberName && a.PassWord == password
                          select a);

            Members members = null;
            if (result.Count() > 0)
            {
                members = result.Single<Members>();
            }

            return members;
        }

        public bool CheckMember(string memberName)
        {
            var q = (from p in DB.PersonalFinanceDB.Members
                     where p.MemberName == memberName
                     select p.MemberID).Distinct();

            return q.Count() > 0;

        }

        public Members SelectMember(int memberID)
        {
            var q = from p in DB.PersonalFinanceDB.Members where p.MemberID == memberID select p;

            Members member = null;

            if (q.Count() > 0)
            {
                member = q.Single<Members>();
            }
            return member;
        }

        public bool InsertMemebers(Members mb)
        {
            DB.PersonalFinanceDB.Members.InsertOnSubmit(mb);
            DB.PersonalFinanceDB.SubmitChanges();

            var q = from p in DB.PersonalFinanceDB.Members
                    where p.MemberName == mb.MemberName
                    select p.MemberID;

            return q.Count() > 0;
        }

        public bool UpdateMembers(Members mb)
        {
            Members m = DB.PersonalFinanceDB.Members.First(c => c.MemberID == mb.MemberID);
            m = mb;
            DB.PersonalFinanceDB.SubmitChanges();

            var q = from p in DB.PersonalFinanceDB.Members
                    where p.MemberID == mb.MemberID
                    select p;

            return q.Single<Members>()==mb;
        }
    }
}
